// packageB/pages/package/task_details_manage/pfef2/temp_package_infos/index.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/pfep2_api",
    row_data: {},
    activeNames: ['1'],
    activeNames1: ['1'],
    sp_show_cg: false,
    sp_show_bzh: false,
    sp_show: false,
    inspect_show: false,
    handle_reason: "",
    handle_action: ""
  },
  // 预览照片包装清单弹窗
  previewPhotopopup(e) {
    console.log(e.currentTarget.dataset.row_data)
    let array = e.currentTarget.dataset.row_data.file_id
    let img_url = []
    img_url.push('https://logisticsengineering.gree.com/pfep2_api/photo_manage/fs_download?file_id=' + array)

    wx.previewImage({
      current: img_url[0],
      urls: img_url,

    })
  },
  onActiveChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  onActiveChange1(event) {
    this.setData({
      activeNames1: event.detail,
    });
  },
  // 审批同意
  inspectShow(e) {

    let key = e.currentTarget.dataset.flag
    console.log(key);
    this.setData({
      inspect_show: true,
      handle_action: key
    })
  },
  oninspecClose() {
    this.setData({
      inspect_show: false,
      handle_action: "",
      handle_reason: "",
    })
  },

  inspectConfirm() {

    let row = this.data.row_data

    let url = ""
    let paload = {}

    if (this.data.sp_show) {
      url = this.data.baseUrl + "/temp_packgae_manage/after_standard_inspect"
      paload = {
        action: this.data.handle_action,
        key: row.task_id,
        reason: this.data.handle_reason,
      }
    } else {
      url = this.data.baseUrl + "/temp_packgae_manage/before_standard_inspect"
      paload = {
        action: this.data.handle_action,
        key: row.task_id,
        process: row.current_process,
        reason: this.data.handle_reason,
        sign_module: [],
        receivers: [],
      }
    }
    console.log(paload);
    this.post(url, paload).then(res => {
      console.log(res);
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/packageB/pages/package/task_manager/task_manager',
          })
        }, 2000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  // 标准化签审人员设置
  inspectSet() {
    let data = this.data.row_data
    wx.navigateTo({
      url: '/packageB/pages/package/task_details_manage/pfef2/temp_package_infos/inspect_set/index',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', data)

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('toHandle', function (data) {
      console.log("传过来的数据", data)
      data.task.show_time = self.GMTToStr1(data.task.start_time) + '~' + self.GMTToStr1(data.task.end_time)
      let show_records = data.task.recorders
      if (data.task.current_involved_name.length != 0) {
        let obj = {
          name: data.task.current_involved_name.join("、"),
          action: '待处理',
          time: "",
          reason: "",

        }
        show_records.push(obj)
      }
      if (data.current_process == "采购员审批") {
        self.setData({
          sp_show_cg: true
        })
      } else if (data.current_process == "标准化") {
        self.setData({
          sp_show_bzh: true
        })
      } else {
        self.setData({
          sp_show: true
        })
      }

      self.setData({
        task_data: data.task,
        row_data: data,
        show_records: show_records.reverse()
      })

    })
    eventChannel.on('showDetail', function (data) {
      console.log("传过来的数据", data)
      data.task.show_time = self.GMTToStr1(data.task.start_time) + '~' + self.GMTToStr1(data.task.end_time)
      let show_records = data.task.recorders
      if (data.task.current_involved_name.length != 0) {
        let obj = {
          name: data.task.current_involved_name.join("、"),
          action: '待处理',
          time: "",
          reason: "",

        }
        show_records.push(obj)
      }
 

      self.setData({
        task_data: data.task,
        row_data: data,
        show_records: show_records.reverse()
      })

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})